package cn.db101.view;

import cn.db101.dao.bookDao;
import cn.db101.model.book;
import cn.db101.model.book_shelf;

import javax.swing.*;
import javax.swing.GroupLayout.Alignment;
import javax.swing.LayoutStyle.ComponentPlacement;
import javax.swing.border.TitledBorder;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;

public class alterbook_Frame{
	JFrame jf=new JFrame() ;
	private ArrayList<book_shelf> res;
	private JTextField bookname_TextField;
	private JTextField bookshelfid_TextField;
	private JTextField bookisbn_TextField;
	private JTextField press_TextField;
	private JTextField bookauthor_TextField;
	private JTextField date_TextField;
	private JComboBox del_ComboBox;
	private JComboBox status_ComboBox;
	private JTextField bookID_TextField;

	/**
	 * Create the frame.
	 * @wbp.parser.entryPoint
	 */
	public void init() {
		jf.setTitle("\u4FEE\u6539\u56FE\u4E66");
		jf.setBounds(100, 100, 743, 735);
		
		JPanel panel = new JPanel();
		jf.getContentPane().add(panel, BorderLayout.WEST);
		
		JLabel lblNewLabel = new JLabel("New label");
		lblNewLabel.setIcon(new ImageIcon(alterbook_Frame.class.getResource("/img/table_head.jpg")));
		
		JPanel panel_1 = new JPanel();
		panel_1.setToolTipText("");
		panel_1.setForeground(new Color(0, 0, 0));
		panel_1.setBorder(new TitledBorder(UIManager.getBorder("TitledBorder.border"), "\u4FEE\u6539图书信息", TitledBorder.LEFT, TitledBorder.TOP, null, new Color(0, 0, 0)));
		GroupLayout gl_panel = new GroupLayout(panel);
		gl_panel.setHorizontalGroup(
			gl_panel.createParallelGroup(Alignment.LEADING)
				.addGroup(gl_panel.createSequentialGroup()
					.addGroup(gl_panel.createParallelGroup(Alignment.LEADING)
						.addComponent(lblNewLabel, GroupLayout.PREFERRED_SIZE, 727, GroupLayout.PREFERRED_SIZE)
						.addGroup(gl_panel.createSequentialGroup()
							.addGap(10)
							.addComponent(panel_1, GroupLayout.PREFERRED_SIZE, 698, GroupLayout.PREFERRED_SIZE)))
					.addContainerGap(324, Short.MAX_VALUE))
		);
		gl_panel.setVerticalGroup(
			gl_panel.createParallelGroup(Alignment.LEADING)
				.addGroup(gl_panel.createSequentialGroup()
					.addComponent(lblNewLabel, GroupLayout.PREFERRED_SIZE, 293, GroupLayout.PREFERRED_SIZE)
					.addPreferredGap(ComponentPlacement.UNRELATED)
					.addComponent(panel_1, GroupLayout.DEFAULT_SIZE, 357, Short.MAX_VALUE)
					.addContainerGap())
		);
		
		JLabel label = new JLabel("\u4E66\u5E93\u72B6\u6001\uFF1A");
		label.setFont(new Font("微软雅黑", Font.PLAIN, 18));
		
		del_ComboBox = new JComboBox();
		del_ComboBox.setFont(new Font("微软雅黑", Font.PLAIN, 18));
		del_ComboBox.addItem("�ڿ���");
		del_ComboBox.addItem("��ɾ��");
		
		
		JLabel label_1 = new JLabel("\u56FE\u4E66\u540D\u79F0\uFF1A");
		label_1.setFont(new Font("微软雅黑", Font.PLAIN, 18));
		
		JLabel label_2 = new JLabel("\u65E5        \u671F\uFF1A");
		label_2.setFont(new Font("微软雅黑", Font.PLAIN, 18));
		
		JLabel label_1_1 = new JLabel("\u4E66\u67B6\u53F7\uFF1A");
		label_1_1.setFont(new Font("微软雅黑", Font.PLAIN, 18));
		
		bookname_TextField = new JTextField();
		bookname_TextField.setColumns(10);
		
		bookshelfid_TextField = new JTextField();
		bookshelfid_TextField.setColumns(10);
		
		JLabel label_2_1 = new JLabel("isbn    \u53F7\uFF1A");
		label_2_1.setFont(new Font("微软雅黑", Font.PLAIN, 18));
		
		bookisbn_TextField = new JTextField();
		bookisbn_TextField.setColumns(10);
		
		JLabel label_2_2 = new JLabel("\u51FA  \u7248  \u793E\uFF1A");
		label_2_2.setFont(new Font("微软雅黑", Font.PLAIN, 18));
		
		press_TextField = new JTextField();
		press_TextField.setColumns(10);
		
		JButton button = new JButton("\u4FEE\u6539");
		button.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
				alterBook();
			}
		});
		button.setIcon(new ImageIcon(alterbook_Frame.class.getResource("/icon/edit\u7F16\u8F91.png")));
		
		JLabel label_1_2 = new JLabel("\u56FE\u4E66\u4F5C\u8005\uFF1A");
		label_1_2.setFont(new Font("微软雅黑", Font.PLAIN, 18));
		
		bookauthor_TextField = new JTextField();
		bookauthor_TextField.setColumns(10);
		
		JLabel label_2_3 = new JLabel("\u72B6        \u6001\uFF1A");
		label_2_3.setFont(new Font("微软雅黑", Font.PLAIN, 18));
		
		status_ComboBox = new JComboBox();
		status_ComboBox.setFont(new Font("微软雅黑", Font.PLAIN, 18));
		status_ComboBox.addItem("������");
		status_ComboBox.addItem("�ѽ��");
		

		
		date_TextField = new JTextField();
		date_TextField.setColumns(10);
		
		JLabel lblid = new JLabel("\u56FE\u4E66ID\uFF1A");
		lblid.setFont(new Font("微软雅黑", Font.PLAIN, 18));
		
		bookID_TextField = new JTextField();
		bookID_TextField.setColumns(10);
		GroupLayout gl_panel_1 = new GroupLayout(panel_1);
		gl_panel_1.setHorizontalGroup(
			gl_panel_1.createParallelGroup(Alignment.LEADING)
				.addGroup(gl_panel_1.createSequentialGroup()
					.addGroup(gl_panel_1.createParallelGroup(Alignment.LEADING)
						.addGroup(gl_panel_1.createSequentialGroup()
							.addGap(59)
							.addGroup(gl_panel_1.createParallelGroup(Alignment.TRAILING)
								.addGroup(gl_panel_1.createParallelGroup(Alignment.LEADING)
									.addGroup(gl_panel_1.createSequentialGroup()
										.addPreferredGap(ComponentPlacement.RELATED)
										.addComponent(label)
										.addGap(18)
										.addComponent(del_ComboBox, 0, GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
									.addGroup(gl_panel_1.createSequentialGroup()
										.addPreferredGap(ComponentPlacement.RELATED)
										.addGroup(gl_panel_1.createParallelGroup(Alignment.TRAILING)
											.addGroup(gl_panel_1.createSequentialGroup()
												.addComponent(label_1_2, GroupLayout.PREFERRED_SIZE, 90, GroupLayout.PREFERRED_SIZE)
												.addGap(18)
												.addComponent(bookauthor_TextField, GroupLayout.PREFERRED_SIZE, 121, GroupLayout.PREFERRED_SIZE))
											.addGroup(gl_panel_1.createSequentialGroup()
												.addComponent(label_1_1, GroupLayout.PREFERRED_SIZE, 86, GroupLayout.PREFERRED_SIZE)
												.addPreferredGap(ComponentPlacement.RELATED)
												.addComponent(bookshelfid_TextField, GroupLayout.PREFERRED_SIZE, 121, GroupLayout.PREFERRED_SIZE)))))
								.addGroup(gl_panel_1.createSequentialGroup()
									.addComponent(label_1, GroupLayout.PREFERRED_SIZE, 90, GroupLayout.PREFERRED_SIZE)
									.addGap(18)
									.addComponent(bookname_TextField, GroupLayout.PREFERRED_SIZE, 121, GroupLayout.PREFERRED_SIZE)))
							.addGap(74)
							.addGroup(gl_panel_1.createParallelGroup(Alignment.LEADING)
								.addGroup(gl_panel_1.createSequentialGroup()
									.addPreferredGap(ComponentPlacement.RELATED)
									.addComponent(label_2)
									.addGap(18)
									.addComponent(date_TextField, GroupLayout.PREFERRED_SIZE, 121, GroupLayout.PREFERRED_SIZE))
								.addGroup(gl_panel_1.createSequentialGroup()
									.addGroup(gl_panel_1.createParallelGroup(Alignment.TRAILING)
										.addGroup(gl_panel_1.createSequentialGroup()
											.addComponent(label_2_3, GroupLayout.PREFERRED_SIZE, 108, GroupLayout.PREFERRED_SIZE)
											.addPreferredGap(ComponentPlacement.RELATED))
										.addGroup(gl_panel_1.createSequentialGroup()
											.addGroup(gl_panel_1.createParallelGroup(Alignment.TRAILING)
												.addComponent(label_2_2, GroupLayout.PREFERRED_SIZE, 94, GroupLayout.PREFERRED_SIZE)
												.addComponent(label_2_1, GroupLayout.PREFERRED_SIZE, 94, GroupLayout.PREFERRED_SIZE))
											.addGap(18)))
									.addGroup(gl_panel_1.createParallelGroup(Alignment.LEADING)
										.addComponent(status_ComboBox, GroupLayout.PREFERRED_SIZE, 121, GroupLayout.PREFERRED_SIZE)
										.addComponent(bookisbn_TextField, GroupLayout.PREFERRED_SIZE, 121, GroupLayout.PREFERRED_SIZE)
										.addComponent(press_TextField, GroupLayout.PREFERRED_SIZE, 121, GroupLayout.PREFERRED_SIZE))))
							.addPreferredGap(ComponentPlacement.RELATED))
						.addGroup(gl_panel_1.createSequentialGroup()
							.addGap(291)
							.addComponent(button))
						.addGroup(gl_panel_1.createSequentialGroup()
							.addGap(212)
							.addComponent(lblid, GroupLayout.PREFERRED_SIZE, 90, GroupLayout.PREFERRED_SIZE)
							.addPreferredGap(ComponentPlacement.RELATED)
							.addComponent(bookID_TextField, GroupLayout.PREFERRED_SIZE, 121, GroupLayout.PREFERRED_SIZE)))
					.addContainerGap(88, Short.MAX_VALUE))
		);
		gl_panel_1.setVerticalGroup(
			gl_panel_1.createParallelGroup(Alignment.LEADING)
				.addGroup(gl_panel_1.createSequentialGroup()
					.addGroup(gl_panel_1.createParallelGroup(Alignment.BASELINE)
						.addComponent(bookID_TextField, GroupLayout.PREFERRED_SIZE, GroupLayout.DEFAULT_SIZE, GroupLayout.PREFERRED_SIZE)
						.addComponent(lblid, GroupLayout.PREFERRED_SIZE, 24, GroupLayout.PREFERRED_SIZE))
					.addGap(28)
					.addGroup(gl_panel_1.createParallelGroup(Alignment.TRAILING)
						.addGroup(gl_panel_1.createParallelGroup(Alignment.BASELINE)
							.addComponent(del_ComboBox, GroupLayout.PREFERRED_SIZE, GroupLayout.DEFAULT_SIZE, GroupLayout.PREFERRED_SIZE)
							.addComponent(label)
							.addComponent(date_TextField, GroupLayout.PREFERRED_SIZE, GroupLayout.DEFAULT_SIZE, GroupLayout.PREFERRED_SIZE))
						.addComponent(label_2, GroupLayout.PREFERRED_SIZE, 24, GroupLayout.PREFERRED_SIZE))
					.addGap(18)
					.addGroup(gl_panel_1.createParallelGroup(Alignment.BASELINE)
						.addComponent(status_ComboBox, GroupLayout.PREFERRED_SIZE, GroupLayout.DEFAULT_SIZE, GroupLayout.PREFERRED_SIZE)
						.addComponent(label_2_3, GroupLayout.PREFERRED_SIZE, 24, GroupLayout.PREFERRED_SIZE)
						.addComponent(bookname_TextField, GroupLayout.PREFERRED_SIZE, GroupLayout.DEFAULT_SIZE, GroupLayout.PREFERRED_SIZE)
						.addComponent(label_1, GroupLayout.PREFERRED_SIZE, 24, GroupLayout.PREFERRED_SIZE))
					.addGap(18)
					.addGroup(gl_panel_1.createParallelGroup(Alignment.BASELINE)
						.addComponent(label_2_1, GroupLayout.PREFERRED_SIZE, 24, GroupLayout.PREFERRED_SIZE)
						.addComponent(bookisbn_TextField, GroupLayout.PREFERRED_SIZE, GroupLayout.DEFAULT_SIZE, GroupLayout.PREFERRED_SIZE)
						.addComponent(label_1_1, GroupLayout.PREFERRED_SIZE, 24, GroupLayout.PREFERRED_SIZE)
						.addComponent(bookshelfid_TextField, GroupLayout.PREFERRED_SIZE, GroupLayout.DEFAULT_SIZE, GroupLayout.PREFERRED_SIZE))
					.addGap(18)
					.addGroup(gl_panel_1.createParallelGroup(Alignment.LEADING)
						.addGroup(gl_panel_1.createSequentialGroup()
							.addGroup(gl_panel_1.createParallelGroup(Alignment.BASELINE)
								.addComponent(bookauthor_TextField, GroupLayout.PREFERRED_SIZE, GroupLayout.DEFAULT_SIZE, GroupLayout.PREFERRED_SIZE)
								.addComponent(label_1_2, GroupLayout.PREFERRED_SIZE, 24, GroupLayout.PREFERRED_SIZE))
							.addGap(65)
							.addComponent(button, GroupLayout.PREFERRED_SIZE, 36, GroupLayout.PREFERRED_SIZE))
						.addGroup(gl_panel_1.createParallelGroup(Alignment.BASELINE)
							.addComponent(press_TextField, GroupLayout.PREFERRED_SIZE, GroupLayout.DEFAULT_SIZE, GroupLayout.PREFERRED_SIZE)
							.addComponent(label_2_2, GroupLayout.PREFERRED_SIZE, 24, GroupLayout.PREFERRED_SIZE)))
					.addGap(47))
		);
		panel_1.setLayout(gl_panel_1);
		panel.setLayout(gl_panel);
		//���ô����Ƿ�ɸı�
		jf.setResizable(false);
		//���ô���ɼ�
		jf.setVisible(true);	
		//����JFrame������ʾ
		jf.setLocationRelativeTo(null);

	}
	
	protected void alterBook() {
		// TODO Auto-generated method stub
		book newbook = new book();
		newbook.setBook_id(bookID_TextField.getText());
		String delstatus = del_ComboBox.getSelectedItem().toString();
		String publishdate = date_TextField.getText();
		newbook.setBook_name(bookname_TextField.getText());
		String bookstatus = status_ComboBox.getSelectedItem().toString();
		
		newbook.setBook_shelf_no(bookshelfid_TextField.getText());
		newbook.setIsbn(bookisbn_TextField.getText());
		newbook.setWriter(bookauthor_TextField.getText());
		newbook.setPublisher(press_TextField.getText());
		
		if(newbook.getBook_id().equals("") || newbook.getBook_id() == null) {
			JOptionPane.showMessageDialog(null, "ͼ��ID����Ϊ��");
			return;
		}
		if(publishdate.equals("") || publishdate == null) {
			newbook.setPubdate(null);
		}
		if(newbook.getBook_name().equals("") || newbook.getBook_name() == null) {
			newbook.setBook_name(null);
		}
		if(newbook.getBook_shelf_no().equals("") || newbook.getBook_shelf_no() == null) {
			newbook.setBook_shelf_no(null);
		}
		if(newbook.getIsbn().equals("") || newbook.getIsbn() == null) {
			newbook.setIsbn(null);
		}
		if(newbook.getWriter().equals("") || newbook.getWriter() == null) {
			newbook.setWriter(null);
		}
		if(newbook.getPublisher().equals("") || newbook.getPublisher() == null) {
			newbook.setPublisher(null);
		}
		if(delstatus.equals("�ڿ���")) {
			newbook.setDel(0);
		}else {
			newbook.setDel(1);
		}
		if(bookstatus.equals("������")) {
			newbook.setBook_status(0);
		}else {
			newbook.setBook_status(1);
		}
		
		SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
		Date date;
		try {
			date = sdf.parse(publishdate);
		}catch (Exception e) {
			// TODO: handle exception
			JOptionPane.showMessageDialog(null, "����yyyy-MM-dd�ĸ�ʽ��д����");
			return;
		}
		if(new bookDao().alterBookInfo(newbook)) {
			JOptionPane.showMessageDialog(null, "�޸ĳɹ�");
			return;
		}else {
			JOptionPane.showMessageDialog(null, "δ�ҵ�����");
			return;
		}
		
	}
	public static void main(String[] args) {
		new alterbook_Frame().init();
	}
}

